package com.hrhx.duhongming.adapter.echarts;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.LineType;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Symbol;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.style.LineStyle;
import com.hrhx.duhongming.adapter.EchartsAdapter;
import com.hrhx.duhongming.bean.EchartsConfig;

/**
 * 
 * @author duhongming
 *
 */
public class EchartsDoubleNumLineAdapter implements EchartsAdapter {

	public static final EchartsDoubleNumLineAdapter ME = new EchartsDoubleNumLineAdapter();

	/**
	 * 适配器入口
	 * 
	 * @param echartsConfig
	 * @param gsonOption
	 * @param data
	 * @return
	 */
	public <T> GsonOption adapter(EchartsConfig echartsConfig, GsonOption gsonOption, Map<String, List<T>> data) {
		gsonOption = ME.tooltipAdapter(gsonOption);
		gsonOption = ME.legendAdapter(gsonOption, data);
		gsonOption = ME.xAxisAdapter(echartsConfig, gsonOption);
		gsonOption = ME.yAxisAdapter(echartsConfig, gsonOption);
		gsonOption = ME.seriesAdapter1(echartsConfig, gsonOption, data);
		return gsonOption;
	}

	/**
	 * List<T>转换为List<Object[]>
	 * @param mapList
	 * @return
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public <T> List<Object[]> convertListObjectToListObjectArray(List<T> mapList){
		List<Object[]> objList = new ArrayList<Object[]>();
		if(mapList != null && mapList.size() != 0){
			T tt = mapList.get(0);
			Class clazz = mapList.get(0).getClass();
			if(tt instanceof Map){
				for (Map<String, Object> map : (List<Map<String, Object>>)mapList) {
					Collection<Object> values = map.values();
					List<Object> list = new ArrayList<>(values);
					objList.add(list.toArray());
				}
			}else if(tt instanceof Object[]){
				objList = (List<Object[]>)mapList;
			}else{
				for (T t : mapList) {
					try {
						Field[] fs = clazz.getDeclaredFields();
						Object[] obj = new Object[2];
						for (int i = 0; i < obj.length; i++) {
							//设置些属性是可以访问的
							fs[i].setAccessible(true);
							//得到此属性的值
							obj[i] = fs[i].get(t);
						}
						objList.add(obj);
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return objList;
	}

	/**
	 * tooltipAdapter
	 * 
	 * @param gsonOption
	 * @return
	 */
	private GsonOption tooltipAdapter(GsonOption gsonOption) {
		gsonOption.calculable(true);
		gsonOption.tooltip().axisPointer().show(true).type(PointerType.cross)
				.lineStyle(new LineStyle().type(LineType.dashed).width(1));
		return gsonOption;
	}

	/**
	 * legendAdapter
	 * 
	 * @param gsonOption
	 * @param data
	 * @return
	 */
	private <T> GsonOption legendAdapter(GsonOption gsonOption, Map<String, List<T>> data) {
		if (data != null && data.size() != 0) {
			// 获取第一个List中的Map中的值
			for (String key : data.keySet()) {
				gsonOption.legend().data(key.replace("y0:", "").replace("y1:", ""));
			}
		}
		return gsonOption;
	}

	/**
	 * x轴配置以及单位配置
	 * 
	 * @param echartsConfig
	 * @param gsonOption
	 * @return
	 */
	private GsonOption xAxisAdapter(EchartsConfig echartsConfig, GsonOption gsonOption) {
		gsonOption.xAxis(new ValueAxis().splitNumber(echartsConfig.getSplitNumber())
				.minInterval(echartsConfig.getMinInterval()).type(AxisType.value).name(echartsConfig.getXaxisName()));
		return gsonOption;
	}

	/**
	 * Y轴单位配置
	 * 
	 * @param echartsConfig
	 * @param gsonOption
	 * @return
	 */
	private GsonOption yAxisAdapter(EchartsConfig echartsConfig, GsonOption gsonOption) {
		String[] unitNameArray = echartsConfig.getYaxisName().split(",");
		for (String s : unitNameArray) {
			ValueAxis categoryAxis = new ValueAxis();
			categoryAxis.type(AxisType.value);
			gsonOption.yAxis(categoryAxis.name(s));
		}
		return gsonOption;
	}
	
	/**
	 * Y轴数据处理
	 * 
	 * @param echartsConfig
	 * @param gsonOption
	 * @param axisDataArr
	 * @return
	 */
	private <T> GsonOption seriesAdapter1(EchartsConfig echartsConfig, GsonOption gsonOption,
			Map<String, List<T>> axisDataArr) {
		for (String mapkey : axisDataArr.keySet()) {
			Bar line = new Bar();
			// 显示直线，而不是密密麻麻的点，一点都不好看
			line.name(mapkey.replace("y0:", "").replace("y1:", "")).type(SeriesType.line).symbol(Symbol.none);
			List<T> dataList = axisDataArr.get(mapkey);
			line.data().addAll(convertListObjectToListObjectArray(dataList));
			if (mapkey.startsWith("y0")) {
				line.yAxisIndex(0);
			} else if (mapkey.contains("y1")) {
				line.yAxisIndex(1);
			}
			gsonOption.series(line);
		}
		return gsonOption;
	}
}
